require("../util.js");
require("../component/components.js");
require("../component/selfmediaflow.js");
require("../../jsMt/util.js");
var cityNameList=JSON.parse(sessionStorage.getItem("cityName"));
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#cityAdInfo",
    data:{
        type:"",
        cityNameList:cityNameList,//城市
        provinceId:"",
        isTab:false,
        topBg:false,
        isflase1:true,
        isflase2:true,
        isNav:2,
        isNav1:2,
        isVideo:true,
        isVideo1:true,
        indexs:0,
        pageNumber:1,
        cityId:"",
        cityName:"",
        categoryId:"",
        cityInfoById:{},
        cityAdvList:{},
        cityAdvList1:[],
        cityAdvList2:[],//人气列表
        release:{}
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.type=this.searchObject&&this.searchObject.type;
        if(this.type&&this.type==1){
            showCityInfo();
        }else{
            this.cityId=this.searchObject&&this.searchObject.cityId;
            this.cityName=decodeURI(this.searchObject&&this.searchObject.cityName);
        }
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //获取城市广告热门城市列表
        getCityInfoById:function(){
            var that=this;
            var data={
                cityId:that.cityId,
                freginCity:this.cityName
            };
            this.ajax({url:"/cityAd/getCityInfoById",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.cityInfoById=b.json.data;
                    getPlayer(that.cityInfoById.officialVideo);
                    getPlayer1(that.cityInfoById.traditionalVideo);
                    // that.categoryId=that.cityInfoById.categorys[0].categoryId;
                    // myPlayer.src(that.cityInfoById.officialVideo);
                    // myPlayer.load();
                    // // myPlayer.play();
                    // console.log(that.cityInfoById.officialVideo)
                    // console.log(that.cityInfoById.traditionalVideo)
                    // myPlayer1.src(that.cityInfoById.traditionalVideo);
                    // myPlayer1.load();
                    // myPlayer1.play();
                    that.getCategoryCityAdvList();
                }else{
                    vm.$refs.prompt.toast("获取信息失败！")
                }
            });

        },
        //获取配置文件
        releaseConfig:function(){
            var that=this,data;
            data={
                loginUid:userInfo.id
            };
            console.log(data)
            this.ajax({url:"/release/releaseConfig",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.release=b.json.data;
                    console.log(that.release)
                }else{
                    vm.$refs.prompt.toast("获取视频信息失败！")
                }
            });
        },
        //tab切换
        centTitleTab:function(categoryId){
            this.categoryId=categoryId;
            this.pageNumber=1;
            this.getCategoryCityAdvList();
        },
        //显示视频等图标
        getIsVideo:function(type){
            console.log(type)
            if(type==1){
                this.isVideo=true;

            }else if(type==2){
                this.isVideo1=true;

            }
            var that = this
            a=setTimeout(function(){
                if(type==1){
                    that.isVideo=false;
                }else if(type==2){
                    that.isVideo1=false;
                }
            },3000);
        },
        //视频暂停
        getVideoPause:function(type){
            console.log(type)
            clearTimeout(a);
            if(type==1){
                this.isNav=2;
                player.pause();
            }else if(type==2){
                this.isNav1=2;
                player1.pause();
            }
        },
        //视频播放
        getVideoPlay:function(type){
            console.log(type)
            // this.isNav=1;
            if(type==1){
                this.isVideo=!this.isVideo;
                this.isflase1=false;
                this.isNav=1;
                // var whereYouAt = myPlayer.currentTime();
                // myPlayer.currentTime(whereYouAt);
                // myPlayer.play();

                player.play();
            }else if(type==2){
                this.isVideo1=!this.isVideo1;
                this.isflase2=false;
                this.isNav1=1;
                // var whereYouAt2 = myPlayer1.currentTime();
                // myPlayer1.currentTime(whereYouAt2);
                // myPlayer1.play();

                player1.play();
            }
        },
        //获取城市广告热门城市列表
        getCategoryCityAdvList:function(){
            var that=this;
            var data={
                cityId:that.cityId,
                freginCity:this.cityName,
                pageNumber:that.pageNumber
            };
            if(that.categoryId!=0){
                data.categoryId=that.categoryId;
            }
            this.ajax({url:"/cityAd/getCategoryCityAdvList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.cityAdvList=b.json.data;
                    that.cityAdvList1=that.cityAdvList.list;
                    if(that.cityAdvList.pageNumber>1){
                        if(that.cityAdvList.list.length>0) {
                            that.cityAdvList2 = that.cityAdvList2.concat(b.json.data.list);
                        }
                    }else{
                        that.cityAdvList2= that.cityAdvList1;
                    }
                    vm.loadFlag=false;
                    // lazyload();
                }else{
                    that.cityAdvList={};
                    that.cityAdvList1=[];
                    that.cityAdvList2=[];
                    vm.$refs.prompt.toast("获取列表失败！")
                }
            });
        },
        //获取中国省市区列表
        getChinaList:function(){
            var that=this;
            this.ajax({url:"/selfMedia/getChinaList",data:{version:that.version},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.cityNameList=b.json.data;
                    that.getCityId();
                    sessionStorage.setItem("chinaList",JSON.stringify(that.chinaList));
                    //console.log(that.chinaList)
                }else{
                    vm.$refs.prompt.toast("获取省市列表失败！")
                }
            });
        },
        getCityId:function(){
            var that=this;
            that.cityNameList.cities.forEach(function(val){
                if(val.name==that.cityName&&val.provinceId==that.provinceId){
                    that.cityId=val.code;
                }
            });
            that.getCityInfoById();
        }
    },watch:{
        cityAdvList2:function(){
            this.$nextTick(function(){
                console.log($(".selfMedia-tabCent").height());
                fmasonry();
                $("img.lazy").lazyload({
                    failurelimit:10,
                    load:fmasonry
                });
                console.log($(".selfMedia-tabCent").height());
            });
        },
        cityNearbyAdvList2:function(){
            this.$nextTick(function(){
                console.log($(".selfMedia-tabCent").height());
                fmasonry();
                $("img.lazy").lazyload({
                    failurelimit:10,
                    load:fmasonry
                });
            });
        },
        cityName:function(){
            var that=this;
            that.releaseConfig();
            if(that.type==1){
                if(that.cityNameList){
                    that.getCityId();
                }else{
                    that.getChinaList();
                }
            }else{
                this.getCityInfoById();
            }
        }
    }
});
var a,player,player1;
function getPlayer(imgSrc){
    player = new Aliplayer({
        id: "my-video",
        "width":"100%",
        "height":"100%",
        //播放方式四：使用STS方式播放
        "vid": imgSrc,//"d388b80a857f4333a39177a5df8005db",//imgSrc,
        "accessKeyId": vm.release.accessKeyId,
        "securityToken": vm.release.securityToken,
        "accessKeySecret": vm.release.accessKeySecret,
        "autoplay": false,
        "isLive": false,
        "rePlay": false,
        "playsinline": true,
        "preload": true,
        "useH5Prism": true
    },function(player){
        console.log('播放器创建好了。')
    });
}
function getPlayer1(imgSrc){
    player1 = new Aliplayer({
        id: "my-video1",
        "width":"100%",
        "height":"100%",
        //播放方式四：使用STS方式播放
        "vid": imgSrc,//"d388b80a857f4333a39177a5df8005db",//imgSrc,
        "accessKeyId": vm.release.accessKeyId,
        "securityToken": vm.release.securityToken,
        "accessKeySecret": vm.release.accessKeySecret,
        "autoplay": false,
        "isLive": false,
        "rePlay": false,
        "playsinline": true,
        "preload": true,
        "useH5Prism": true
    },function(player){
        console.log('播放器创建好了。')
    });
}
// var myPlayer = videojs('my-video',{
//     loop:true,
//     // poster:vm.cityInfoById.officialCoverImgSrc,//预览图：string
//     autoplay:false,//自动播放：boolean
//     controlBar: {
//         //fullscreenToggle:false,
//         volumeMenuButton:false
//     }
// });
// var myPlayer1 = videojs('my-video1',{
//     loop:true,
//     // poster:vm.cityInfoById.traditionalCoverImgSrc,
//     autoplay: false,
//     controlBar: {
//         //fullscreenToggle:false,
//         volumeMenuButton:false
//     }
// });
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
var map = new AMap.Map('iCenter', {
    resizeEnable: true
});
//获取用户所在城市信息
function showCityInfo() {
    //实例化城市查询类
    var citysearch = new AMap.CitySearch();
    //自动获取用户IP，返回当前城市
    citysearch.getLocalCity(function(status, result) {
        console.log(result);
        if (status === 'complete' && result.info === 'OK') {
            if (result && result.city) {
                vm.provinceId=result.adcode;
                vm.cityName=result.city;
            }
        } else {
            // vm.cityId=result.adcode;
            vm.cityName=result.info;
        }
    });
}
window.onscroll=function(){
    if(getDocumentTop()>0){
        vm.topBg=true;
    }else{
        vm.topBg=false;
    }
    if(getDocumentTop()>640){
        vm.isTab=true;
    }else{
        vm.isTab=false;
    }
    var loadFlag = vm.cityAdvList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            vm.getCategoryCityAdvList();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function fmasonry() {
    minigrid('.selfMedia-tabCent', '.selfMedia-list',9);
    // $(".selfMedia-tabCent").masonry({
    //     gutterWidth: 10,
    //     itemSelector: '.selfMedia-list',
    //     isAnimated: true,
    //     isFitWidth:true
    // });
}
function lazyload(){
    $("img.lazy").lazyload();
}